package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.CommentService;
import com.woniu.adopt.vo.CommentVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController // responsebody + controller
@RequestMapping("comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    //模糊查询 （救助站名称，地址）+ 分页
    @GetMapping("/listComment")
    @ApiOperation(value = "本接口按条件分页查询评论数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页",dataType = "int",paramType = "query",name = "now"),
            @ApiImplicitParam(value = "分页单位",dataType = "int",paramType = "query",name = "size"),
            @ApiImplicitParam(value = "按照用户名称条件查询",dataType = "String",paramType = "query",name = "uname"),
            @ApiImplicitParam(value = "按照宠物名称模糊查询条件",dataType = "String",paramType = "query",name = "pname")
    })
    public ResultResponse<PageInfo<CommentVo>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "3")Integer size,
            String uname, String pname){

        ResultResponse<PageInfo<CommentVo>> result = null;
        try {
            PageInfo<CommentVo> pageInfo = commentService.queryCommentByPage(now, size, uname,pname);
            result = new ResultResponse<>(200,"查询成功!",pageInfo);
            System.out.println(pageInfo+"   ==================");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }


    //修改返显
    @GetMapping("/getCommentByKey/{id}")
    @ApiOperation(value = "本接口按id查询评论数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "按照id查询评论", dataType = "int", paramType = "query", name = "id")
    })
    public ResultResponse<CommentVo> getCommentByKey(@PathVariable("id") int id) {

        ResultResponse<CommentVo> result = null;
        try {
            CommentVo commentVo = commentService.selectCommentByKey(id);
            result = new ResultResponse<>(200, "查询评论成功!", commentVo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询评论失败!");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "回复用户评论")
    public ResultResponse<Void> modify(@RequestBody CommentVo commentVo) {
        ResultResponse<Void> result = null;
        try {
            //回复评论时间
            commentVo.setUpdateTime(new Date());
            commentService.updateComment(commentVo);
            System.out.println(commentVo+"    ---------------------");
            result = new ResultResponse<>(200, "回复成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "回复失败！");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据救助站的标识ID 逻辑删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "int",name = "id",value = "评论的ID标识")
    })
    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            int i = commentService.removeComment(id);
            if(i == 1){
                result = new ResultResponse<>(200,"删除评论成功!");
            }else{
                result = new ResultResponse<>(201,"删除评论失败");
            }

        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(203,"系统错误，请联系管理员!");
        }
        return result ;
    }
}
